<?php
/**
 * Recupere la configuration du site, cette fonction utilise la connexion PDO
 * @return <type>
 */
function getConfig()
{
    global $config;

    $setting = array();

    $q = $GLOBALS['db']->query("SELECT * FROM ". PREFIX ."config");
    $s = $q->fetchAll();
    $q->closeCursor();

    foreach($s as $set):
        $setting[''. $set['name'] .''] = $set['setting'];
    endforeach;

    $setting['url_site'] = $config['url'];

    return $setting;
}

/**
 * Retourne l alternance
 * @return <type>
 */
function getAlternance($indice)
{
    if($indice % 2 == 1)
        return 1;
    else
        return 2;
}

function getAgenceSelect()
{
   $req = $GLOBALS['db']->prepare("SELECT id, nom FROM ". PREFIX ."agence ORDER BY nom");
   $req->execute();

   return $req->fetchAll();
}

function getTypeSelect()
{
    $req = $GLOBALS['db']->prepare("SELECT id, nom FROM ". PREFIX ."annonce_type WHERE visible = 1 ORDER BY nom");
    $req->execute();

    return $req->fetchAll();
}

function getSecteurSelect($withAnnonce = true)
{
    if($withAnnonce):
        $Query = "SELECT DISTINCT ase.id, ase.nom FROM immo_annonce_secteur ase INNER JOIN immo_annonce a ON ase.id = a.secteur WHERE a.active = 1 ORDER BY nom";
    else:
        $Query = "SELECT id, nom FROM immo_annonce_secteur ORDER BY nom";
    endif;

    $req = $GLOBALS['db']->prepare($Query);
    $req->execute();

    return $req->fetchAll();
}


function get_form_select_agence($agence_default = null)
{
    $agences = array();

    if( is_array($agence_default) && !is_null($agence_default) ):
        foreach($agence_default as $key => $value):
            $agences[$key] = $value;
        endforeach;
    endif;

    $a = getAgenceSelect();

    foreach($a as $agence):
        $agences[$agence['id']] = $agence['nom'];
    endforeach;

    $GLOBALS['smarty']->assign('agence_option', $agences);
}

function get_form_select_type($type_default = null)
{
    $types = array();

    if( is_array($type_default) ):
        foreach($type_default as $key => $value):
            $types[''. $key .''] = $value;
        endforeach;
    endif;

   $t = getTypeSelect();

   foreach($t as $type):
       $types[$type['id']] = $type['nom'];
   endforeach;

    $GLOBALS['smarty']->assign('type_option', $types);
}

function get_form_select_prix($prix_default = null)
{
    if( is_array($prix_default)):
        foreach($prix_default as $key => $value):
            $Prix[$key] = $value;
        endforeach;
    endif;

    $Prix[1] = htmlentities('0 ? 100 000');
    $Prix[2] = htmlentities('100 000 ? 200 000');
    $Prix[3] = htmlentities('200 000 +');

    $GLOBALS['smarty']->assign('prix_option', $Prix);
}

function get_form_select_secteur($secteur_default = null, $withAnnonce = true)
{
    $secteurs = array();

    if( is_array($secteur_default) ):
        foreach($secteur_default as $key => $value):
            $secteurs[$key] = $value;
        endforeach;
    endif;

    $s = getSecteurSelect($withAnnonce);

    foreach($s as $secteur):
        $secteurs[$secteur['id']] = $secteur['nom'];
    endforeach;

    $GLOBALS['smarty']->assign('secteur_option', $secteurs);
}

function get_form_select_departement($departement_default = null)
{
    $departements = array();

    require IMMOPHP_ROOT_PATH . 'core'. DS .'departements.php';

    if( is_array($departement_default) ):
        foreach($departement_default as $key => $value):
            $departements[$key] = $value;
        endforeach;
    endif;

    foreach($departement_liste as $key => $value):
        $departements[$key] = $key .' '. $value;
    endforeach;

    $GLOBALS['smarty']->assign('departement_option', $departements);
}
?>